home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / comms / other / amarquee / examples / amarqueedebug.c < prev    next >
C/C++ Source or Header  |  1999-05-25  |  7KB  |  185 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13.  
  14. struct Library * AMarqueeBase = NULL;
  15. struct QSession * session     = NULL;
  16.  
  17. /* Takes user input until blank line is typed */
  18. void ProcessDebugCommands(struct QSession * session)
  19. {
  20.   while(1)
  21.   {
  22.     char debugline[500] = "\0\0\0\0\0\0\0\0";
  23.     char * keyword, * data;
  24.     ULONG dataLen = 0L;
  25.     LONG res;
  26.     
  27.     printf("Enter your debug command now: "); fflush(stdout);
  28.     gets(debugline);
  29.   
  30.     keyword = &debugline[2];
  31.     if (data = strchr(keyword,'='))
  32.     {
  33.       *data = '\0';  /* terminate the keyword */
  34.       data++;
  35.       dataLen = strlen(data)+1;
  36.     }
  37.     switch((int)(debugline[0]))
  38.     {
  39.       case '\0': res=QGo(session,0L);                           break;
  40.       case 'A':  res=QSetMessageAccessOp(session, keyword, -1); break;
  41.       case 'm':  res=QMessageOp(session, keyword, data, dataLen); break;
  42.       case 'M':  res=QSysMessageOp(session, keyword, data);   break;
  43.       case 'a':  res=QSetAccessOp(session, keyword);          break;
  44.         case 'x':  res=QSetKeyAccessOp(session, keyword, data);          break;
  45.         case 's':  res=QSetOp(session, keyword, data, dataLen); break;
  46.       case 'S':  res=QStreamOp(session, keyword, data, dataLen); break;
  47.       case 'r':  res=QRenameOp(session, keyword, data);       break;
  48.       case 'D':  res=QDebugOp(session, keyword);              break;
  49.       case 'g':  res=QGetOp(session, keyword, -1);            break;
  50.       case 'd':  res=QDeleteOp(session, keyword);             break;
  51.       case 'i':  res=QInfoOp(session);                        break;
  52.       case 'c':  res=QSubscribeOp(session, keyword, -1);      break;
  53.       case 'C':  res=QGetAndSubscribeOp(session, keyword, -1);     break;
  54.       case 'k':  res=QClearSubscriptionsOp(session,atoi(keyword)); break;
  55.       case 'p':  res=QPingOp(session);                             break;
  56.       case 'v':  res=QRequestPrivilegesOp(session,atol(keyword));  break;
  57.       case 'w':  res=QReleasePrivilegesOp(session,atol(keyword));  break;
  58.       case '!':  res=QKillClientsOp(session,keyword);              break;
  59.       case '?':  res=QGetParameterOp(session,keyword);             break;
  60.       case '$':  res=QSetParameterOp(session,keyword,data);        break;
  61.       default:   printf("Command code %c was not recognized.\n",debugline[0]); break;
  62.     }
  63.     printf("(Op result was %i)\n",res);
  64.     if (debugline[0] == '\0') return;
  65.   }
  66. }
  67.  
  68.  
  69. void CleanExit(void)
  70. {
  71. printf("\nCleaning up...\n");
  72.   if (session)      QFreeSession(session);        /* This MUST be done before we close the library! */
  73.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);  
  74.   printf("All done.\n");
  75. }
  76.  
  77. /* Main program */
  78. int main(int argc, char ** argv)
  79. {
  80.   char * connectTo, * progName;
  81.   int port;
  82.      
  83.   printf("Usage Note:  AMarqueeDebug [hostname=localhost] [myname=debug] [port=2957]\n");  
  84.   atexit(CleanExit);
  85.   
  86.   connectTo = (argc>1) ? argv[1] : "localhost";
  87.   progName  = (argc>2) ? argv[2] : "debug";
  88.   port      = (argc>3) ? atoi(argv[3]) : 2957;
  89.  
  90.   if ((AMarqueeBase = OpenLibrary("amarquee.library",46L)) == NULL)
  91.   {
  92.      printf("Couldn't open amarquee.library v46!\n");
  93.      exit(RETURN_ERROR);
  94.   }
  95.   printf("Connecting to %s:%i\n",connectTo, port);
  96.  
  97. #ifdef ASYNC_CONNECT
  98.   if ((session = QNewSessionAsync(connectTo, port, progName)) == NULL)
  99.   {
  100.      printf("Couldn't connect to server %s:%i\n",connectTo, port);
  101.      exit(RETURN_WARN);
  102.   }
  103. #else
  104.   if ((session = QNewSession(connectTo, port, progName)) == NULL)
  105.   {
  106.      printf("Couldn't connect to server %s:%i\n",connectTo, port);
  107.      exit(RETURN_WARN);
  108.   }
  109. #endif
  110.  
  111.   printf("Connected to server %s:%i\n",connectTo, port);
  112.  
  113. #ifdef WANT_INSTANT_SAMPLE_DATA
  114.   /* Setup some sample data */
  115.   (void)QSetOp(session, "sampledata",             "Kids",    5);
  116.   (void)QSetOp(session, "sampledata/Jeremy",      "1",       2);
  117.   (void)QSetOp(session, "sampledata/Joanna",      "2",       2);
  118.   (void)QSetOp(session, "sampledata/Joanna/nerd", "yep! :)", 8);
  119.   (void)QSetOp(session, "sampledata/Joellen",     "3",       2);
  120.   (void)QSetOp(session, "sampledata/Charcoal",    "Kitty!",  7);
  121.   (void)QGo(session, 0L);
  122. #endif
  123.  
  124.   printf("Commands are:\n");
  125.   printf("\n");
  126.   printf("a wildhostpath   Default Access control (default is /#?/#?)\n");
  127.   printf("x path=wildhostpath  Access control for single paths (default is /#?/#?)\n");
  128.   printf("A wildhostpath   Access control for incoming messages (default in no access)\n");
  129.   printf("m hosts=data     Send an active message to hosts\n");
  130.   printf("M hosts=string   Send an system message to hosts\n");
  131.   printf("s path=data      Set data node value\n");
  132.   printf("S path=data      Stream data node value\n");
  133.   printf("r path=newlabel  Rename data node\n");
  134.   printf("D debugstring    Send debug string\n");
  135.   printf("g wildpath       Get a node or nodes\n");
  136.   printf("c wildpath       Subscribe to a node or nodes\n");
  137.   printf("C wildpath       Get&Subscribe to a node or nodes\n");
  138.   printf("k opID           Klear subscriptions (by id or 0 for all)\n");
  139.   printf("d wildpath       Delete a node or nodes\n");
  140.   printf("i                Request info packet\n");
  141.   printf("p                Request ping packet\n");
  142.   printf("v #              Request new privileges (by code bitchord)\n");
  143.   printf("w #              Release existing privileges (by code bitchord)\n");
  144.   printf("! hosts          Kill other clients (requires KILLCLIENTS privilege!)\n");
  145.   printf("$ param=string   Set a parameter with QSetParam()\n");
  146.   printf("? param          Get a parameter by name\n");
  147.   printf("<enter>          Send accumulated transactions (GO!!)\n");
  148.   printf("\n");
  149.   printf("Press CTRL-F to enter commands, or CTRL-C to quit\n");
  150.   
  151.   while(1)
  152.   {
  153.      struct QMessage * qMsg;
  154.      ULONG signals = (1L << session->qMsgPort->mp_SigBit) | (SIGBREAKF_CTRL_C) | (SIGBREAKF_CTRL_F);
  155.  
  156.      /* Wait for next message from the server */
  157.      signals = Wait(signals);
  158.      
  159.      if (signals & (1L << session->qMsgPort->mp_SigBit))
  160.      {
  161.         while(qMsg = (struct QMessage *) GetMsg(session->qMsgPort))
  162.         {
  163.           struct QRunInfo * inf = (qMsg->qm_DataLen == sizeof(struct QRunInfo)) ? ((struct QRunInfo *) qMsg->qm_Data) : NULL;
  164.  
  165.           /* Handle message */
  166.           printf("Message %p recieved---------\n",qMsg);
  167.           printf("Status:       %i (%s)\n",  qMsg->qm_Status, QErrorName(qMsg->qm_Status));
  168.           printf("Error Line:   %i\n",  qMsg->qm_ErrorLine);
  169.           printf("Message ID:   %i\n",  qMsg->qm_ID);
  170.           printf("Path:        [%s]\n", qMsg->qm_Path?qMsg->qm_Path:"<NULL>");
  171.           printf("Data:        [%s](int=%i)\n", qMsg->qm_Data?qMsg->qm_Data:((UBYTE*)"<NULL>"),qMsg->qm_Data?(*((int*)qMsg->qm_Data)):0);
  172.           printf("DataLen:      %lu\n", qMsg->qm_DataLen);
  173.           printf("ActualLen:    %lu\n", qMsg->qm_ActualLen);
  174.  
  175.           if (inf) printf("Info: alloced:%li allowed:%li avail:%li curPrivs=0x%x posPrivs=0x%x\n",
  176.              inf->qr_Alloced, inf->qr_Allowed, inf->qr_Avail, inf->qr_CurrentPrivs, inf->qr_PossiblePrivs); 
  177.           FreeQMessage(session, qMsg);
  178.         }
  179.      }
  180.      if (signals & SIGBREAKF_CTRL_F) ProcessDebugCommands(session);
  181.      if (signals & SIGBREAKF_CTRL_C) break;  /* Quit if CTRL-C pressed */
  182.   }
  183.   /* CleanExit() called here! */
  184. }
  185.